<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mongo+Node</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css"
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }

        form {
            width: 30%;
            margin: 0 auto;
        }

        img {
            width: 100%;
        }

        .btn {
            width: 100%;
        }

        h1 {
            margin: 0 auto;
            text-align: center;
            padding: 100px 0 50px 0;
        }

        .nodata {
            display: block;
            text-align: center;
            margin: 0 auto;
            color: #bdbdbd;
        }

        .files {
            display: flex;
            flex-wrap: wrap;
        }

        .card {
            margin-right: 20px;
        }
    </style>
</head>

<body>
    <h1 class="display-4">UPLOAD BOX</h1>
    <form method="POST" action="/upload" enctype="multipart/form-data">
        <div class="input-group mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text" id="inputGroupFileAddon01">UPLOAD</span>
            </div>
            <div class="custom-file">
                <input type="file" class="custom-file-input" name="file" id="inputGroupFile01"
                    aria-describedby="inputGroupFileAddon01" onchange="handleFileChange(this)" accept="image/gif, image/jpeg, image/png, image/jpg">
                <label class="custom-file-label" for="inputGroupFile01">选择文件</label>
            </div>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
    <br>
    <br>
    <div class="files">
        <% if(files){ %>            
            <% files.forEach(function(file){ %>
            <div class="card" style="width: 18rem;">
                <img src="image/<%=file.filename%>" class="card-img-top" alt="<%=file.filename%>">
                <div class="card-body">
                    <h5 class="card-title"><%=file.filename%></h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
                        card's content.</p>
                    <form action="/files/<%=file._id%>?_method=delete" method="POST">
                        <button type="submit" class="btn btn-danger btn-sm">删除</button>
                    </form>
                </div>
            </div>
            <% }) %>            
            <% }else { %>
            <p class="nodata">暂无文件</p>
            <% } %> 
    </div>
</body>
<!-- 引入组件库 -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
    integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
    integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"
    integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous">
</script>
<script>
    function handleFileChange(e) {
        const arr = e.value.split('\\')
        document.getElementsByClassName('custom-file-label')[0].innerText = arr[arr.length - 1]
    }
</script>

</html>